package p03_SingleLinkedList;

public class HeroList {
    private HeroNode head = new HeroNode(0, "", "");
    public HeroNode getHead() {
        return head;
    }

    private HeroNode getLastNode() {
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        return temp;
    }

    public void add(HeroNode node) {
        HeroNode lastNode = getLastNode();
        lastNode.next = node;
    }

    /**
     * //第二种方式在添加英雄时，根据排名将英雄插入到指定位置
     * //(如果有这个排名，则添加失败，并给出提示）
     * @param node
     */
    public void addByOrder(HeroNode node) {
        HeroNode temp = head;
        // 获取添加位置的节点
        while (true) {
            if (temp.next == null || temp.next.no > node.no) {
                break;
            }
            if (temp.no == node.no) {
                System.out.println("添加失败，英雄序号已存在！");
                return;
            }
            temp = temp.next;
        }
        // 开始添加
        node.next = temp.next;
        temp.next = node;
    }

    public void print() {
        if (head.next == null) {
            System.out.println("空");
            return;
        }

        HeroNode temp = head.next;
        while (true) {
            System.out.println(temp);
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
    }

    /**
     * 修改节点的信息，根据no编号来修改，即no编号不能改*
     * @param node
     */
    public void update(HeroNode node) {
        if (isEmpty()) {
            System.out.println("空");
            return;
        }
        // 查找出要求改的节点
        HeroNode temp = head.next;
        while (true) {
            if (temp == null) {
                System.out.println("英雄序号不在列表中！");
                return;
            }
            if (temp.no == node.no) {
                break;
            }
            temp = temp.next;
        }
        temp.name = node.name;
        temp.nickName = node.nickName;
    }

    public boolean isEmpty() {
        return head.next == null;
    }

    public void delete(int no) {
        if (isEmpty()) {
            System.out.println("空");
            return;
        }
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                System.out.println("序号不在列表中！");
                return;
            }
            if (temp.next.no == no) {
                break;
            }
            temp = temp.next;
        }
        temp.next = temp.next.next;
    }

    public HeroNode get(int no) {
        HeroNode temp = head;
        while (true) {
            if (temp == null) {
                return null;
            }
            if (temp.no == no) {
                return temp;
            }
            temp = temp.next;
        }
    }
}

class HeroNode {
    public int no;
    public String name;
    public String nickName;
    protected HeroNode next;

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + "'" +
                '}';
    }
}
